package cn.chair.service;

import cn.chair.util.EmptyUtils;
import life.innovation.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

@Service
public class DeviceService {


    @Autowired
    @Lazy
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 查询设备是否正在使用中，1=使用中=true
     */
    public boolean getDeviceisRun(String deviceId)
    {
        ValueOperations<String, String> valOps = stringRedisTemplate.opsForValue();
        String data = valOps.get(deviceId);
        return EmptyUtils.isNotEmpty(data);
    }
    /**
     * 查询设备是否在线，2=空闲状态=true
     */
    public boolean  getDeviceisOnline(String deviceName)
    {
        String key = "heartbeat" + ":" + deviceName;
        ValueOperations<String, String> valOps = stringRedisTemplate.opsForValue();
        String data = valOps.get(key);
        if(EmptyUtils.isNotEmpty(data) && data.equals("1"))
        {
            return  true;
        }else{
            return  false;
        }
    }
}
